package com.itheima.controller;

import com.itheima.R;
import com.itheima.dto.CreateCouponDto;
import com.itheima.service.CouponService;
import com.itheima.vo.PageResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * @author Hakuna Matata
 * @date 2022-10-11-11:20
 */
@RestController
@RequestMapping("coupon")
public class CouponController {

    @Autowired
    private CouponService couponService;

    /**
     * 新增优惠券
     *
     * @param createCouponDto
     * @return
     */
    @PostMapping("create")
    public R createCoupon(@RequestBody CreateCouponDto createCouponDto) {
        couponService.createCoupon(createCouponDto);

        return R.success("优惠券新增成功");
    }

    /**
     * 删除优惠券
     *
     * @param id
     * @return
     */
    @PostMapping("delete/{id}")
    public R deleteCoupon(@PathVariable("id") Integer id) {
        couponService.deleteCoupon(id);
        return R.success("删除优惠券成功");
    }

    /**
     * 根据优惠券名称和类型分页获取优惠券列表
     */
    @GetMapping("list")
    public R getCouponListByCouponNameAndByPage(String name,
                                                @RequestParam(value = "pageNum", defaultValue = "1") Integer pageNum,
                                                @RequestParam(value = "pageSize", defaultValue = "10") Integer pageSize,
                                                Integer type) {

        PageResult pageResult = couponService.getCouponListByCouponNameAndByPage(name, pageNum, pageSize, type);
        return R.success(pageResult);
    }

    /**
     * 修改优惠券
     *
     * @param id
     * @param createCouponDto
     * @return
     */
    @PostMapping("update/{id}")
    public R updateCoupon(@PathVariable("id") Integer id, @RequestBody CreateCouponDto createCouponDto) {

        couponService.updateCoupon(id, createCouponDto);
        return R.success("更新优惠券成功");
    }

    /**
     * 获取单个优惠券的详细信息
     *
     * @param id
     * @return
     */
    @GetMapping("{id}")
    public R updateCoupon(@PathVariable("id") Integer id) {
        CreateCouponDto createCouponDto = couponService.findCouponById(id);
        return R.success(createCouponDto);
    }
}
